1. Resources

R for Data Science: https://r4ds.had.co.nz

R cheatsheets: https://rstudio.com/resources/cheatsheets/

Base R cheatsheet: https://rstudio.com/wp-content/uploads/2016/10/r-cheat-sheet-3.pdf

ggplot cheatsheet: https://rstudio.com/wp-content/uploads/2015/03/ggplot2-cheatsheet.pdf

2. Create an R Project

https://r4ds.had.co.nz/workflow-projects.html

8.4 RStudio projects

R experts keep all the files associated with a project together — input data, R scripts, analytical results, figures. This is such a wise and common practice that RStudio has built-in support for this via projects.

Let’s make a project for you to use while you’re working through the rest of this book. Click File > New Project, then:

A project is an entity that contains:

Create a new project every time you start a new project

3. R Studio

A quick orientation to your R studio windows:

A more detailed orientation to R Studio:

4. R script

You should create a new R Script every time you are working on a new task within your project. Think about the R script as a word document that you can write in, edit, save, then open later to work on again. To open up an R script go to New File > R Script.

A new window will open up called “Untitled1”

Type this code into your R script:

a <- 3
b <- 4

a + b

What happened when you pressed enter after a + b? Did you get this error message: “Error: object ‘a’ not found”?

Why didn’t it work? If you just pressed enter after each line, you just went to a new line of your code. In order for R to know what “a” and “b” are, you need to execute this code so that R knows that you are assigning the value “3” to the variable “a” and the value “4” to the variable “b.”

To execute your code, put your cursor back on the first line or highlight “a <- 3” and press command + enter (mac) or control + enter (windows). Do the same for “b <- 4” and “a + b.” Now what happened? You should have gotten an output in your console that “a + b” equals 7.

Look at your R environment. Before you executed your code, this window should have been empty but now after executing your code, you have two new variables, a and b. R will save these values in your environment for each working session, which means that later on in your code if you refer to “a” again, R will remember that “a = 3.” However, if you close your R script or R project, it will not save your environment unless you tell it to. In general, it’s good practice not to save any information between working sessions but rather to start fresh. There are two reasons for this. First, in your next session, you may start working in a new R script and you may want to assign “a” to a new value. Second, your code should always be 100% reproducible. For example, if you call “a + b” without first assigning “a” in this code, then you should get the error message “Error: object ‘a’ not found” to remind you that you need to assign “a” a value.

Sometimes you may want to clear your environment within a session, particularly if you’re switching from working on one R script to another. You can do so with this command:

remove(list = ls())

4. Plotting

Okay, now let’s do something fun. Let’s make a plot!!!

First we need some data. R has a number of datasets already loaded that you can access. You can find a description of them here: https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/00Index.html

A really popular dataset is called “iris” and contains measurements on flower sepal length, sepal width, petal length, and petal width for several species of flower.

Let’s take a quick look at these data. There are several ways to do this:

Option 1: Call the data

iris
##     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
## 1            5.1         3.5          1.4         0.2     setosa
## 2            4.9         3.0          1.4         0.2     setosa
## 3            4.7         3.2          1.3         0.2     setosa
## 4            4.6         3.1          1.5         0.2     setosa
## 5            5.0         3.6          1.4         0.2     setosa
## 6            5.4         3.9          1.7         0.4     setosa
## 7            4.6         3.4          1.4         0.3     setosa
## 8            5.0         3.4          1.5         0.2     setosa
## 9            4.4         2.9          1.4         0.2     setosa
## 10           4.9         3.1          1.5         0.1     setosa
## 11           5.4         3.7          1.5         0.2     setosa
## 12           4.8         3.4          1.6         0.2     setosa
## 13           4.8         3.0          1.4         0.1     setosa
## 14           4.3         3.0          1.1         0.1     setosa
## 15           5.8         4.0          1.2         0.2     setosa
## 16           5.7         4.4          1.5         0.4     setosa
## 17           5.4         3.9          1.3         0.4     setosa
## 18           5.1         3.5          1.4         0.3     setosa
## 19           5.7         3.8          1.7         0.3     setosa
## 20           5.1         3.8          1.5         0.3     setosa
## 21           5.4         3.4          1.7         0.2     setosa
## 22           5.1         3.7          1.5         0.4     setosa
## 23           4.6         3.6          1.0         0.2     setosa
## 24           5.1         3.3          1.7         0.5     setosa
## 25           4.8         3.4          1.9         0.2     setosa
## 26           5.0         3.0          1.6         0.2     setosa
## 27           5.0         3.4          1.6         0.4     setosa
## 28           5.2         3.5          1.5         0.2     setosa
## 29           5.2         3.4          1.4         0.2     setosa
## 30           4.7         3.2          1.6         0.2     setosa
## 31           4.8         3.1          1.6         0.2     setosa
## 32           5.4         3.4          1.5         0.4     setosa
## 33           5.2         4.1          1.5         0.1     setosa
## 34           5.5         4.2          1.4         0.2     setosa
## 35           4.9         3.1          1.5         0.2     setosa
## 36           5.0         3.2          1.2         0.2     setosa
## 37           5.5         3.5          1.3         0.2     setosa
## 38           4.9         3.6          1.4         0.1     setosa
## 39           4.4         3.0          1.3         0.2     setosa
## 40           5.1         3.4          1.5         0.2     setosa
## 41           5.0         3.5          1.3         0.3     setosa
## 42           4.5         2.3          1.3         0.3     setosa
## 43           4.4         3.2          1.3         0.2     setosa
## 44           5.0         3.5          1.6         0.6     setosa
## 45           5.1         3.8          1.9         0.4     setosa
## 46           4.8         3.0          1.4         0.3     setosa
## 47           5.1         3.8          1.6         0.2     setosa
## 48           4.6         3.2          1.4         0.2     setosa
## 49           5.3         3.7          1.5         0.2     setosa
## 50           5.0         3.3          1.4         0.2     setosa
## 51           7.0         3.2          4.7         1.4 versicolor
## 52           6.4         3.2          4.5         1.5 versicolor
## 53           6.9         3.1          4.9         1.5 versicolor
## 54           5.5         2.3          4.0         1.3 versicolor
## 55           6.5         2.8          4.6         1.5 versicolor
## 56           5.7         2.8          4.5         1.3 versicolor
## 57           6.3         3.3          4.7         1.6 versicolor
## 58           4.9         2.4          3.3         1.0 versicolor
## 59           6.6         2.9          4.6         1.3 versicolor
## 60           5.2         2.7          3.9         1.4 versicolor
## 61           5.0         2.0          3.5         1.0 versicolor
## 62           5.9         3.0          4.2         1.5 versicolor
## 63           6.0         2.2          4.0         1.0 versicolor
## 64           6.1         2.9          4.7         1.4 versicolor
## 65           5.6         2.9          3.6         1.3 versicolor
## 66           6.7         3.1          4.4         1.4 versicolor
## 67           5.6         3.0          4.5         1.5 versicolor
## 68           5.8         2.7          4.1         1.0 versicolor
## 69           6.2         2.2          4.5         1.5 versicolor
## 70           5.6         2.5          3.9         1.1 versicolor
## 71           5.9         3.2          4.8         1.8 versicolor
## 72           6.1         2.8          4.0         1.3 versicolor
## 73           6.3         2.5          4.9         1.5 versicolor
## 74           6.1         2.8          4.7         1.2 versicolor
## 75           6.4         2.9          4.3         1.3 versicolor
## 76           6.6         3.0          4.4         1.4 versicolor
## 77           6.8         2.8          4.8         1.4 versicolor
## 78           6.7         3.0          5.0         1.7 versicolor
## 79           6.0         2.9          4.5         1.5 versicolor
## 80           5.7         2.6          3.5         1.0 versicolor
## 81           5.5         2.4          3.8         1.1 versicolor
## 82           5.5         2.4          3.7         1.0 versicolor
## 83           5.8         2.7          3.9         1.2 versicolor
## 84           6.0         2.7          5.1         1.6 versicolor
## 85           5.4         3.0          4.5         1.5 versicolor
## 86           6.0         3.4          4.5         1.6 versicolor
## 87           6.7         3.1          4.7         1.5 versicolor
## 88           6.3         2.3          4.4         1.3 versicolor
## 89           5.6         3.0          4.1         1.3 versicolor
## 90           5.5         2.5          4.0         1.3 versicolor
## 91           5.5         2.6          4.4         1.2 versicolor
## 92           6.1         3.0          4.6         1.4 versicolor
## 93           5.8         2.6          4.0         1.2 versicolor
## 94           5.0         2.3          3.3         1.0 versicolor
## 95           5.6         2.7          4.2         1.3 versicolor
## 96           5.7         3.0          4.2         1.2 versicolor
## 97           5.7         2.9          4.2         1.3 versicolor
## 98           6.2         2.9          4.3         1.3 versicolor
## 99           5.1         2.5          3.0         1.1 versicolor
## 100          5.7         2.8          4.1         1.3 versicolor
## 101          6.3         3.3          6.0         2.5  virginica
## 102          5.8         2.7          5.1         1.9  virginica
## 103          7.1         3.0          5.9         2.1  virginica
## 104          6.3         2.9          5.6         1.8  virginica
## 105          6.5         3.0          5.8         2.2  virginica
## 106          7.6         3.0          6.6         2.1  virginica
## 107          4.9         2.5          4.5         1.7  virginica
## 108          7.3         2.9          6.3         1.8  virginica
## 109          6.7         2.5          5.8         1.8  virginica
## 110          7.2         3.6          6.1         2.5  virginica
## 111          6.5         3.2          5.1         2.0  virginica
## 112          6.4         2.7          5.3         1.9  virginica
## 113          6.8         3.0          5.5         2.1  virginica
## 114          5.7         2.5          5.0         2.0  virginica
## 115          5.8         2.8          5.1         2.4  virginica
## 116          6.4         3.2          5.3         2.3  virginica
## 117          6.5         3.0          5.5         1.8  virginica
## 118          7.7         3.8          6.7         2.2  virginica
## 119          7.7         2.6          6.9         2.3  virginica
## 120          6.0         2.2          5.0         1.5  virginica
## 121          6.9         3.2          5.7         2.3  virginica
## 122          5.6         2.8          4.9         2.0  virginica
## 123          7.7         2.8          6.7         2.0  virginica
## 124          6.3         2.7          4.9         1.8  virginica
## 125          6.7         3.3          5.7         2.1  virginica
## 126          7.2         3.2          6.0         1.8  virginica
## 127          6.2         2.8          4.8         1.8  virginica
## 128          6.1         3.0          4.9         1.8  virginica
## 129          6.4         2.8          5.6         2.1  virginica
## 130          7.2         3.0          5.8         1.6  virginica
## 131          7.4         2.8          6.1         1.9  virginica
## 132          7.9         3.8          6.4         2.0  virginica
## 133          6.4         2.8          5.6         2.2  virginica
## 134          6.3         2.8          5.1         1.5  virginica
## 135          6.1         2.6          5.6         1.4  virginica
## 136          7.7         3.0          6.1         2.3  virginica
## 137          6.3         3.4          5.6         2.4  virginica
## 138          6.4         3.1          5.5         1.8  virginica
## 139          6.0         3.0          4.8         1.8  virginica
## 140          6.9         3.1          5.4         2.1  virginica
## 141          6.7         3.1          5.6         2.4  virginica
## 142          6.9         3.1          5.1         2.3  virginica
## 143          5.8         2.7          5.1         1.9  virginica
## 144          6.8         3.2          5.9         2.3  virginica
## 145          6.7         3.3          5.7         2.5  virginica
## 146          6.7         3.0          5.2         2.3  virginica
## 147          6.3         2.5          5.0         1.9  virginica
## 148          6.5         3.0          5.2         2.0  virginica
## 149          6.2         3.4          5.4         2.3  virginica
## 150          5.9         3.0          5.1         1.8  virginica

Option 2: See just the first 5 rows of the data

head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

or first 20 rows

head(iris, 10)
##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1           5.1         3.5          1.4         0.2  setosa
## 2           4.9         3.0          1.4         0.2  setosa
## 3           4.7         3.2          1.3         0.2  setosa
## 4           4.6         3.1          1.5         0.2  setosa
## 5           5.0         3.6          1.4         0.2  setosa
## 6           5.4         3.9          1.7         0.4  setosa
## 7           4.6         3.4          1.4         0.3  setosa
## 8           5.0         3.4          1.5         0.2  setosa
## 9           4.4         2.9          1.4         0.2  setosa
## 10          4.9         3.1          1.5         0.1  setosa

or last 20 rows:

tail(iris, 20)
##     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
## 131          7.4         2.8          6.1         1.9 virginica
## 132          7.9         3.8          6.4         2.0 virginica
## 133          6.4         2.8          5.6         2.2 virginica
## 134          6.3         2.8          5.1         1.5 virginica
## 135          6.1         2.6          5.6         1.4 virginica
## 136          7.7         3.0          6.1         2.3 virginica
## 137          6.3         3.4          5.6         2.4 virginica
## 138          6.4         3.1          5.5         1.8 virginica
## 139          6.0         3.0          4.8         1.8 virginica
## 140          6.9         3.1          5.4         2.1 virginica
## 141          6.7         3.1          5.6         2.4 virginica
## 142          6.9         3.1          5.1         2.3 virginica
## 143          5.8         2.7          5.1         1.9 virginica
## 144          6.8         3.2          5.9         2.3 virginica
## 145          6.7         3.3          5.7         2.5 virginica
## 146          6.7         3.0          5.2         2.3 virginica
## 147          6.3         2.5          5.0         1.9 virginica
## 148          6.5         3.0          5.2         2.0 virginica
## 149          6.2         3.4          5.4         2.3 virginica
## 150          5.9         3.0          5.1         1.8 virginica

Option 3: View entire dataset in a separate window

View(iris)

Here’s a quick checklist about what you should look at whenever you get a new dataset:

str(iris)
## 'data.frame':    150 obs. of  5 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

Scatter plot

Let’s say we want to look at how sepal length is related to sepal width

library(tidyverse)

iris %>% 
  ggplot(aes(x = Sepal.Length, Sepal.Width)) +
  geom_point()

Your plot should pop up in the Plots window. You can press Zoom to get a better view of the plot. Or click Export and save it.

We can make this figure more complex:

iris %>% 
  ggplot(aes(x = Sepal.Length, Sepal.Width, color = Species)) +
  geom_point()

We can add a fitted line if we think there is a linear relationship between sepal width and sepal length:

iris %>% 
  ggplot(aes(x = Sepal.Length, Sepal.Width, color = Species)) +
  geom_point() +
  geom_smooth(method = "lm")

We can also make this figure more beautiful:

theme_set( #theme_set will make this setting for all plots within this session
  theme_bw() + #this will make the background black and white instead of gray
  theme(panel.grid = element_blank()) #this will remove the grid lines 
  )

iris %>% 
  ggplot(aes(x = Sepal.Length, Sepal.Width, color = Species)) +
  geom_point() +
  geom_smooth(method = "lm")

We can also label the x and y axes:

iris %>% 
  ggplot(aes(x = Sepal.Length, Sepal.Width, color = Species)) +
  geom_point() +
  geom_smooth(method = "lm") +
  xlab("Sepal Length (cm)") +
  ylab("Sepal Width (cm)")

We can also make the points bigger and adjust the shape or shading:

iris %>% 
  ggplot(aes(x = Sepal.Length, Sepal.Width, color = Species)) +
  geom_point(size = 2, alpha = 0.5) +
  geom_smooth(method = "lm") +
   geom_smooth(method = "lm") +
  xlab("Sepal Length (cm)") +
  ylab("Sepal Width (cm)")

iris %>% 
  ggplot(aes(x = Sepal.Length, Sepal.Width, color = Species)) +
  geom_point(size = 2, shape = 1) +
  geom_smooth(method = "lm") +
   geom_smooth(method = "lm") +
  xlab("Sepal Length (cm)") +
  ylab("Sepal Width (cm)")

Instead of noting species by color, we could can change the shape and lineytpe by species (good if you need to print the figure in black and white):

iris %>% 
  ggplot(aes(x = Sepal.Length, Sepal.Width, shape = Species, linetype = Species)) +
  geom_point() +
  geom_smooth(method = "lm") +
   geom_smooth(method = "lm") +
  xlab("Sepal Length (cm)") +
  ylab("Sepal Width (cm)")

Bar plots

Making a bar plot is not immediately intuitive in R. First, you should know that bar plots give you the ability to plot the summary of your data unless you specify to plot the data as is. Let me show by example…

Let’s say you want to plot the sample size by species (in other words, how many individuals of each species was measured in this dataset?):

iris %>% 
  ggplot(aes(x = Species)) +
  geom_bar()

The figure isn’t very exciting, but it’s important to understand what’s happening behind the scenes. Take a minute to interpret this figure. What is it telling us? And what exactly happened when we asked R to plot a bar graph with “species” on the x-axis?

The figure above tells us that there were 50 individuals for each of the 3 species in our iris dataset. When we called the ggplot + geom_bar code, what R did was it counted how many entries our data had for each Species, or in other words, how many times did setosa, versicolor, and virginica appear in our dataset. It did the math for us, and then plotted it! That’s pretty cool.

What if we want to make a bar plot with something like mean sepal length by species?

First we need to do the math:

iris %>% 
  group_by(Species) %>% 
  summarise(mean_sepal_length = mean(Sepal.Length))
## # A tibble: 3 x 2
##   Species    mean_sepal_length
##   <fct>                  <dbl>
## 1 setosa                  5.01
## 2 versicolor              5.94
## 3 virginica               6.59

Then we plot it:

iris %>% 
  group_by(Species) %>% 
  summarise(mean_sepal_length = mean(Sepal.Length)) %>% 
  ggplot(aes(x = Species, y = mean_sepal_length)) +
  geom_bar(stat = "identity") 

Notice that now we have to specify a y-axis and specify inside our geom_bar call that the statistic (“stat”) to use is the actual number in that column (“identity”).

Notice also that I first had to summarize the data iris to produce a mean value for sepal length (mean_sepal_length) and then I piped it into the plotting function. I could also have done this:

iris_mean_sl <- iris %>% 
  group_by(Species) %>% 
  summarise(mean_sepal_length = mean(Sepal.Length)) 

iris_mean_sl %>% 
  ggplot(aes(x = Species, y = mean_sepal_length)) +
  geom_bar(stat = "identity") 

How do the two variations of the code differ and what is the advantage to doing one over the other?

We could leave this lesson on bar plots here, but that would be unsafitisfying beccause we are missing a CRITICAl component to the figure above. Can anyone guess what that is???

One should NEVER show a mean value with showing the variability around that mean. Write it down, tattoo on your arm, use it as your daily mantra!

Here is our complete figure with mean and standard error bars. I have also added a couple stylistic elements - I changed the alpha (shdaing) on the fill of the bars to 0.5, which makes it look nicer, and I adjusted the width of the upper and lower bars on the errorbar to 0.1. You can play with both these values to see how it changes the aesthetics of your figure:

iris %>% 
  group_by(Species) %>% 
  summarise(mean_sepal_length = mean(Sepal.Length),
            sd_sepal_length = sd(Sepal.Length), 
            n = n(),
            se_sepal_length = sd_sepal_length/sqrt(n)) %>% 
  ggplot() +
  geom_bar(aes(x = Species, y = mean_sepal_length), 
           stat = "identity",
           alpha = 0.5) + 
  geom_errorbar(aes(x = Species, 
                    ymin = mean_sepal_length - se_sepal_length, 
                    ymax = mean_sepal_length + se_sepal_length),
                width = 0.1) + #width adjusts the width of the upper and lower bars of the errorbar
  ylab("Mean Sepal Length (cm)")

5. Mapping!

There were some people interested in mapping. Mapping is becoming more accessible in R. Many people still use ArcGIS and while it can do a lot, R is quickly catching up AND one of the benefits to using R is that it is open source software, meaning you don’t have to pay for it and that anyone can write a package to add to mapping/spatial functionality in R, so it is constantly evolving new capabilities!

Before we get started, you’ll need to install/read in these libraries and download this dataset.

library(tidyverse)
library(sf)
library(leaflet)

Then we need to read it in. To find out where your file lives, use this command:

file.choose()

Copy this directory and paste it inside the command read_csv()

scholar_locations <- read_csv("insert_your_file_path_here")

Eventually you’ll want to store this file in a data folder that is in your R project and then you can read it in using a relative path rather than a direct path. But we can talk about this later… Let’s take a quick look at it:

scholar_locations
## # A tibble: 20 x 4
##    name                   university                          latitude longitude
##    <chr>                  <chr>                                  <dbl>     <dbl>
##  1 Isaac Aguilar          UC Berkeley                             37.9    -122. 
##  2 Cristobal Castaneda    UC Davis                                38.5    -122. 
##  3 Jean Nikolas Clemente  Bowdoin College                         43.9     -70.0
##  4 Jennifer Delgado       Carleton College                        44.5     -93.2
##  5 Michael Angelo Fernan… University of Guam                      13.4     145. 
##  6 Kathryn Hart           Indiana University                      39.2     -86.5
##  7 Kian Kafaie            Brown University                        41.8     -71.4
##  8 Tahiry Langrand        Arizona State University                32.9    -112. 
##  9 Adriana Maciel Metal   Yale University                         41.3     -72.9
## 10 Shantae McIntyre       University of Connecticut               41.8     -72.3
## 11 Amina Muumin           University of Minnesota                 45.0     -93.2
## 12 Kellie Navarro         Bowdoin College                         43.9     -70.0
## 13 Genesis Perez          Inter American University of Puert…     18.2     -66.3
## 14 Marion Powell          Willamette University                   44.9    -123. 
## 15 Amy Quintanilla        Wellesly College                        42.3     -71.3
## 16 Sheila Marie Ringor    University of Hawa'I at Manoa           21.3    -158. 
## 17 Isabella Rosado        Yale University                         41.3     -72.9
## 18 Fana Scott             Eckerd College                          27.7     -82.7
## 19 Joshua Stewart         University of Miami                     25.7     -80.3
## 20 Valerie Tafoya         DePaul University                       41.9     -87.7

Let’s map our locations! There are several ways to do this in R, but this one is my favorite.

First we will use functions from package sf to turn our dataset into a spatial features dataframe:

scholar_locations_sf <- st_as_sf(scholar_locations, coords = c("longitude", "latitude"), crs = 4326)

Then we map it, and check it out, you can move the map and zoom in and out!

scholar_locations_sf %>% 
  leaflet() %>% 
  addTiles() %>% 
  addCircleMarkers()

We can refine this map to make it prettier by setting the view (setView) to center the map and to change the underlying map layer:

scholar_locations_sf %>% 
  leaflet() %>% 
  setView(lng = -100, lat = 40, zoom = 3) %>% 
  addProviderTiles(providers$Stamen.Watercolor) %>% 
  addCircleMarkers()

You can take a look at all the different types of basemaps available and switch them out with providers$Stamen.Watercolor: https://leaflet-extras.github.io/leaflet-providers/preview/

We could also label the circles to see who is where:

scholar_locations_sf %>% 
  leaflet() %>% 
  setView(lng = -100, lat = 40, zoom = 3) %>% 
  addProviderTiles(providers$Stamen.Watercolor) %>% 
  addCircleMarkers(label = ~name)

We could look at who is where and what university they are at:

scholar_locations_sf %>% 
  leaflet() %>% 
  setView(lng = -100, lat = 40, zoom = 3) %>% 
  addProviderTiles(providers$Stamen.Watercolor) %>% 
  addCircleMarkers(label = ~paste(name, university, sep = "; "))

We have a problem with a number of scholars that are going to the same school. This part is pretty fancy but actually not that difficult to implement. We can cluster points with two more lines of code:

scholar_locations_sf %>% 
  leaflet() %>% 
  setView(lng = -100, lat = 40, zoom = 3) %>% 
  addProviderTiles(providers$Stamen.Watercolor) %>% 
  addCircleMarkers(label = ~paste(name, university, sep = "; "),
                   group = ~university, #this tells leaflet what column to cluster by
                   clusterOptions = markerClusterOptions(removeOutsideVisibleBounds = F)) #this clusters the points

6. General R conventions

  1. List all the libraries you will need to run your code at the top of your code.

  2. Follow the tidyverse style guide (https://style.tidyverse.org/index.html)

Naming: - short but informative - use lowercase as much as you can - never use a space or period (.) to separate words; you can use "_" (tree_data) or camelcase (treeData) - whatever you use, be consistent and don’t mix naming conventions (don’t do: tree_Data)

Coding: - use spaces between words/commands and enter new lines to keep your code clean - make comments using # to help you remember what you did